Skip to content

Add cloud writing for records and annotations - #694

Merged
tompollard merged 2 commits into
mainfrom
issue-517-cloud-writing
Nov 24, 2026
Merged

tompollard merged 2 commits into
mainfrom
issue-517-cloud-writing

Conversation

@tompollard

Copy link
Copy Markdown
Member

Summary

  • Add cloud URI support to write_dir for wrsamp and wrann, including single and multi-segment records.
  • Stage files locally, then upload with fsspec, publishing the main header last.
  • Document supported storage backends, local disk needs, and the lack of atomic multi-file replacement.

Closes #517.

Validation

  • pytest -m 'not network' -q: 696 passed, 4 skipped, 20 deselected
  • Ruff check and format check: passed
  • Sphinx HTML build with -W --keep-going: passed

The new write tests use memory:// and cover records, multi-segment records, annotations, failed validation, and overwrite warnings.

@tompollard tompollard left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, with nothing blocking. One test would be worth adding before merging, and a few small tidy-ups.

What I checked:

  • Upload order: for a multi-segment record written to memory://, I recorded each path opened for writing. The order was v102s.dat, then v102s_1.hea, then v102s.hea, so the main header is last, and only the record's files were in the folder.
  • Nothing is published after an error: a check that fails (an invalid fmt) uploads nothing, since the error is raised before the upload loop.
  • The overwrite warning works in the cloud: it checks the cloud folder, and reads the record's existing header from it. A write_dir with a trailing slash works.
  • Tests: pytest -m "not network" (696 passed) and ruff pass.

Worth adding:

  1. A test for the upload order. Uploading the main header last is the property that keeps readers from finding a half-uploaded record, but no test covers it. A test could patch wfdb.io._cloud._fsspec_open to record the paths opened for writing, write a multi-segment record, and check that the main header comes last.

Minor:

  1. physionet:// is rejected late in Record.wrsamp. _warn_about_replacing_other_files runs before staged_cloud_write rejects physionet://, so it first asks PhysioNet whether the signal files exist: rec.wrsamp(write_dir="physionet://mitdb/1.0.0") takes about 1.1 s before raising. Offline it still raises the right ValueError. Rejecting physionet:// at the top of wrsamp would avoid the requests, and the test could cover Record.wrsamp as well as wrann.
  2. One misleading error. An open local WFDBArchive with a cloud write_dir raises "Writing .wfdb archives to cloud storage is unsupported", although the archive is local. Rejecting the combination is fine, but the message could say that write_dir isn't used when writing to an open archive.
  3. Duplicated checks. The two archive checks are repeated in Record.wrsamp and MultiRecord.wrsamp. A small helper in _cloud.py (for example check_cloud_write(write_dir, wfdb_archive)) would keep them consistent, and could include the physionet:// check from 2.

@tompollard

Copy link
Copy Markdown
Member Author

Thanks for the thorough review. I addressed all four points in 33931a9:

  • The multi-segment memory:// test now records upload paths and checks that the main header is uploaded last.
  • physionet:// destinations are rejected before the overwrite lookup; a Record.wrsamp test verifies that no lookup occurs.
  • Combining a cloud write_dir with an open local WFDBArchive now gives a specific error explaining that the directory is unused.
  • The shared check_cloud_write helper handles these destination and archive checks for both record writers.

Validation: 697 offline tests passed (4 skipped), Ruff passed, and the strict Sphinx build passed.

@tompollard tompollard left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-reviewed 33931a9. All four points are addressed, and this is ready to merge.

  1. Upload order: test_multi_record_wrsamp_to_cloud records every path opened for writing, and checks that the main header comes last. I confirmed that it fails if staged_cloud_write uploads the main header first.
  2. physionet://: check_cloud_write rejects it before the overwrite lookup. Record.wrsamp(write_dir="physionet://mitdb/1.0.0") now fails in 0.000 s, instead of after about 1.1 s of PhysioNet requests, and the test checks that no lookup happens.
  3. An open archive with a cloud write_dir now raises a clear "cloud write_dir is unused with an open WFDBArchive" error, and a test covers it.
  4. Duplicated checks: both wrsamp methods and Annotation.wrann use the shared check_cloud_write. They are the only callers of staged_cloud_write, so no write path skips the check now that staged_cloud_write no longer does it itself.

pytest -m "not network" (697 passed), ruff and the docs build with warnings as errors all pass.

@tompollard
tompollard merged commit f947ac6 into main Nov 24, 2026
18 checks passed
@tompollard
tompollard deleted the issue-517-cloud-writing branch November 24, 2026 18:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for reading and writing WFDB files in cloud containers

1 participant